package com.sghd.candy.module.player.facade;

import com.sghd.candy.module.bag.manager.PlayerBag;
import com.sghd.candy.module.bag.manager.PlayerBagManager;
import com.sghd.candy.module.bag.service.PlayerBagService;
import com.sghd.candy.module.element.manager.PlayerElement;
import com.sghd.candy.module.element.service.PlayerElementService;
import com.sghd.candy.module.fight.manager.LevelInfo;
import com.sghd.candy.module.fight.service.FightService;
import com.sghd.candy.module.fight.service.LevelInfoService;
import com.sghd.candy.module.player.manager.Player;
import com.sghd.candy.module.player.manager.PlayerManager;
import com.sghd.candy.module.player.service.PlayerService;
import com.sghd.candy.module.skill.service.PlayerSkillService;
import com.sghd.pojo.StaticConfig;
import com.sghd.pojo.candy.bag.enums.PropType;
import com.sghd.pojo.candy.fight.vo.ServerVo;
import com.sghd.pojo.candy.player.vo.BaseReq;
import com.sghd.pojo.candy.player.vo.InitVo;
import com.sghd.pojo.candy.player.vo.ReqPlayerVo;
import com.sghd.pojo.fight.game.LevelVo;
import com.sghd.pojo.fight.game.PlayerVo;
import com.sghd.pojo.fight.game.Result;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.HashMap;
import java.util.Map;

/**
 * @author Allen
 * @date 2018/12/07
 */
@Component
public class PlayerFacadeImpl implements PlayerFacade {
    private static final Logger logger = LoggerFactory.getLogger(PlayerFacadeImpl.class);
    @Autowired
    private PlayerSkillService playerSkillService;
    @Autowired
    private PlayerService playerService;
    @Autowired
    private PlayerBagService playerBagService;
    @Autowired
    private PlayerElementService playerElementService;
    @Autowired
    private LevelInfoService levelInfoService;
    @Autowired
    private FightService fightService;

    @Override
    public InitVo getInitInfo(long playerId) {

        Player player = playerService.load(playerId);

        if (player == null) {
            return null;
        }

        InitVo initVo = new InitVo();
        initVo.setPlayerId(player.getPlayerId());
        initVo.setName(player.getNickName());
        initVo.setSex(player.getSex());
        initVo.setHeadIconId(player.getHeadIconId());
        initVo.setRenamed(player.isRenamed());
        LevelInfo levelInfo = levelInfoService.getLevelInfo(player);
        initVo.setLevelGrade(levelInfo.getLevelGrade());
        initVo.setLevelVo(new LevelVo(levelInfo.getBigLevelId(), levelInfo.getLevelId()));

        PlayerBag playerBag = playerBagService.getPlayerBag(playerId);
        Map<Integer, Long> propMap = new HashMap(2);
        propMap.put(PropType.MONEY.ordinal(), playerBag.getMoney());
        propMap.put(PropType.DIAMOND.ordinal(), playerBag.getDiamond());
        initVo.setPropDetail(propMap);

        ServerVo serverVo = fightService.getFightServer(player);

        if (serverVo.getReConn()) {
            initVo.setFightServer(serverVo);
        }

        //引导进度
        initVo.setGuide(playerService.loadOther(player, StaticConfig.PLAYER_GUIDE, 0, Integer.class));
        return initVo;
    }

    @Override
    public PlayerVo getFightPlayerInfo(String ip, ReqPlayerVo req) {

        Player player = playerService.load(req.getId());
        if (player == null) {
            if (logger.isDebugEnabled()) {
                logger.debug("玩家ID[{}]，不存在！", req.getId());
            }
            return null;
        }
        PlayerVo playerVo = PlayerVo.valueOf(req.getSn(), player.getPlayerId(), player.getNickName(), player.getHeadIconId());

        Map<Integer, Integer> skillMap = playerSkillService.getFightSkill(player.getPlayerId(), req);
        System.out.println(skillMap);
        playerVo.setSkills(skillMap);

        PlayerElement playerElement = playerElementService.getPlayerElement(player.getPlayerId());
        playerVo.setElements(playerElement.getElements());

        PlayerBag playerBag = playerBagService.getPlayerBag(player.getPlayerId());
        playerVo.setMoney(playerBag.getMoney());

        LevelInfo levelInfo = levelInfoService.getLevelInfo(player);
        playerVo.setLevelVo(new LevelVo(levelInfo.getBigLevelId(), levelInfo.getLevelId()));

        return playerVo;
    }

    @Override
    public Result<String> modifyHeadIcon(long playerId, Integer headIcon) {
        return playerService.modifyHeadIcon(playerService.load(playerId), headIcon);
    }

    @Override
    public Result<String> rename(long playerId, String nickName) {
        return playerService.rename(playerService.load(playerId), nickName);
    }

    @Override
    public Result<String> guide(long playerId, Integer guide) {
        Player player = playerService.load(playerId);
        return playerService.guide(player, guide);
    }


}
